﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Entity.Table;
using SqlHelper;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class CheckLedgerDatabase
    {
        /// <summary>
        /// 添加一条总帐信息
        /// </summary>
        /// <param name="ledger">总帐数据实体</param>
        /// <returns></returns>
        public static bool Insert(TB_CheckLedger ledger)
        {
            bool ret = true;
            SqlConnection conn = null;

            SqlParameter[] paramList = new SqlParameter[] { 
                                                            DbHelper.CreateCommandParameter("@BankCode",SqlDbType.VarChar,ledger.BankCode),
                                                            DbHelper.CreateCommandParameter("@BusinessDate",SqlDbType.VarChar,ledger.BusinessDate),
                                                            DbHelper.CreateCommandParameter("@StartTime",SqlDbType.VarChar,ledger.StartTime),
                                                            DbHelper.CreateCommandParameter("@EndTime",SqlDbType.VarChar,ledger.EndTime),
                                                            DbHelper.CreateCommandParameter("@PayTotalMoney",SqlDbType.Decimal,ledger.PayTotalMoney),
                                                            DbHelper.CreateCommandParameter("@PayStrokeCount",SqlDbType.Int,ledger.PayTotalCount),
                                                            DbHelper.CreateCommandParameter("@BackoutMoney",SqlDbType.Decimal,ledger.CancelTotalMoney),
                                                            DbHelper.CreateCommandParameter("@BackoutStrokeCount",SqlDbType.Int,ledger.CancelTotalCount),
                                                            DbHelper.CreateCommandOutParameter("@return",SqlDbType.Int)
                                                           };
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                ret = DbHelper.ExecuteNonQueryByProc("CheckLedger_Insert_Sp", paramList, conn) > 0;
            }
            catch (SqlException ex)
            {
                ret = false;
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }

        /// <summary>
        /// 根据条件获取总帐信息。
        /// </summary>       
        /// <returns></returns>
        public static DataTable GetGeneralAccount(string bankCode,string businessDate, string startTime,string endTime)
        {
            DataTable dt = null;
            SqlConnection conn = null;

            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@BankCode",SqlDbType.VarChar,bankCode),
                                                            DbHelper.CreateCommandParameter("@BusinessDate", SqlDbType.VarChar, businessDate),
                                                            DbHelper.CreateCommandParameter("@StartTime",SqlDbType.VarChar,startTime),
                                                            DbHelper.CreateCommandParameter("@EndTime",SqlDbType.VarChar,endTime)
                                                          };

            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                dt = DbHelper.GetTableByProcedure("GetGeneralAccount_Sp", paramList, conn);
            }
            catch (SqlException ex)
            {
                dt = null;
            }
            finally
            {
                conn.Close();
            }

            return dt;
        }
    }
}
